package org.scc.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.scc.model.Resource;
import org.scc.model.Role;
import org.scc.model.User;

public class RoleDao extends BaseDao{

	@Override
	protected String getMapper(String methodName) {
		return "org.scc.sqlmap.RoleMapper."+methodName;
	}
	
	/**
	 * 添加角色
	 * @param role
	 */
	public void add(Role role){
		insert(getMapper("add"), role, "add role error");
	}
	
	/**
	 * 更新角色
	 * @param role
	 */
	public void update(Role role){
		update(getMapper("update"), role, "update role error");
	}
	
	/**
	 * 删除角色
	 * @param ids
	 */
	public void delete(String ids){
		delete(getMapper("delete"), ids, "delete role error");
	}
	
	/**
	 * 查询所有角色
	 * @return
	 */
	public List<Role> findAll(){
		return selectList(getMapper("findAll"), "findAll role error");
	}
	
	/**
	 * 加载一个角色
	 * @param id
	 * @return
	 */
	public Role load(int id){
		return selectOne(getMapper("load"), id, "load Group error");
	}
	
	/**
	 * 给角色绑定资源
	 * @param roleId
	 * @param resourceIds
	 */
	public void addResource(int roleId, String resourceIds){
		String[] ids = resourceIds.split(",");
		Map<String, Integer> params = new HashMap<String, Integer>();
		for(String sid : ids){
			params.put("roleId", roleId);
			params.put("resourceId", Integer.parseInt(sid) );
			insert(getMapper("addResource"), params, "add role resource error");
			params.clear();
		}
	}
	
	public void addUsers(int roleId, String userIds){
		String[] ids = userIds.split(",");
		Map<String, Integer> params = new HashMap<String, Integer>();
		for(String sid : ids){
			params.put("roleId", roleId);
			params.put("userId", Integer.parseInt(sid) );
			insert(getMapper("addUser"), params, "add role user error");
			params.clear();
		}
	}
	
	public void removeRes(int roleId, String resIds){
		String[] ids = resIds.split(",");
		Map<String, Integer> params = new HashMap<String, Integer>();
		for(String sid : ids){
			params.put("roleId", roleId);
			params.put("resourceId", Integer.parseInt(sid) );
			delete(getMapper("deleteResource"), params, "remove role resource error");
			params.clear();
		}
	}
	
	public void removeUsers(int roleId, String userIds){
		String[] ids = userIds.split(",");
		Map<String, Integer> params = new HashMap<String, Integer>();
		for(String sid : ids){
			params.put("roleId", roleId);
			params.put("userId", Integer.parseInt(sid) );
			delete(getMapper("deleteUser"), params, "remove role user error");
			params.clear();
		}
	}
	
	/**
	 * 查询角色所有的资源
	 * @param roleId
	 * @return
	 */
	public List<Resource> findResource(int roleId){
		return selectList(getMapper("findResource"), roleId, "find role resource error");
	}
	
	/**
	 * 查询角色所有的二级菜单
	 * @param roleId
	 * @return
	 */
	public List<Resource> findSLResource(int roleId){
		return selectList(getMapper("findSLRoleRes"), roleId, "find role resource error");
	}
	
	public List<Role> findScopeAll(){
		return selectList(getMapper("findByScopeAll"), "find scope all role error");
	}
	
	public List<Role> findUserRole(int userId){
		return selectList(getMapper("findUserRoles"), userId, "find user's roles error");
	}
	
	public List<User> findRoleUsers(int roleId){
		return selectList(getMapper("findUsers"), roleId, "find role's users error");
	}
	
}
